#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 1e7+7;
const int INF = 0x3f3f3f3f;
int a[MAXN]; //数组
int maxrating[MAXN];
int cnt[MAXN];
int que[MAXN];
LL n, m, k, p, q, r, MOD;
int Next(LL ask, LL i) {
	return (p*ask + q*i + r) % MOD;
}
void Process() {
    int beg = n, en = n+1;
    que[en] = n+1;
    que[beg] = n;
    int len = n - m + 1;
    for(int i=n; i>len; --i) {
        while(a[que[beg]] <= a[i]) ++beg;
        que[--beg] = i;
    }
    for (int i=n-m+1; i>=1; --i) {
        while(beg <= en && a[que[beg]] <= a[i]) ++beg;
        que[--beg] = i;
        if (que[en] == i+m) --en;
        cnt[i] = en-beg+1;
        maxrating[i] = a[que[en]];
    }
//    for (int i=1; i<n-m+1; ++i) {
//        printf("%d %d\n", maxrating[i], cnt[i]);
//    }
}
int main() {
#ifdef hai
    freopen("in.txt", "r", stdin);
#endif
	int T;
	cin>>T;
	while (T--) {
		scanf("%lld %lld %lld %lld %lld %lld %lld", &n, &m, &k, &p, &q, &r, &MOD);
		LL t;
		for (int i=1; i<=k; ++i) {
			scanf("%d", a+i);
		}
		for (int i=k+1; i<=n; ++i) {
			a[i] = Next(a[i-1], i);
		}
        a[n+1] = INF;
        Process();
        int len = n-m+1;
        LL A=0, B=0;
        for (int i=1; i<=len; ++i) {
            A += (maxrating[i]^i);
            B += (cnt[i]^i);
        }
        printf("%lld %lld\n", A, B);
	}
	return 0;
}
